package com.aos.erp.schools.rest.notice;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.common.utils.StringUtils;
import com.aos.erp.schools.remote.AosService;
import com.aos.erp.schools.remote.model.NoticeInfo;
import com.aos.erp.schools.remote.model.dto.NoticeDetailDTO;
import com.aos.erp.schools.remote.model.vo.NoticeConditionVo;
import com.aos.erp.schools.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@Api(tags = {"UI接口：通知公告"})
@RestController
@RequestMapping("/api/aos/notice")
public class RestNoticeController {

    @Resource
    private AosService aosService;

    @ApiOperation(value = "查询所有公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "noticeTitle", value = "公告标题", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "noticeContent", value = "公告内容", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "fromTime", value = "发布开始时间", dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "toTime", value = "发布截止时间", dataType = "String", paramType = "Query")
    })
    @GetMapping("/list")
    public ResultEntity<PageBean<NoticeDetailDTO>> listNotice(NoticeConditionVo params, String currentUserId) throws Exception {
        long orgId = 480;
        String dsId = UserUtil.getUserSchoolId(currentUserId);
        if(StringUtils.isNotEmpty(dsId)) {
            orgId = Long.parseLong(dsId);
        } else {
            throw new BizException("您无权查看公告");
        }
        params.setOrgId(orgId);
        PageBean<NoticeDetailDTO> dataPageBean = aosService.listNotice(params);
        return ResultEntity.success(dataPageBean);
    }

    @ApiOperation(value = "查看公告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "公告ID", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/preview")
    public ResultEntity<NoticeInfo> previewNotice(Long dataId){
        NoticeInfo noticeInfo = aosService.previewNotice(dataId);
        return ResultEntity.success(noticeInfo);
    }
}
